from db_connect import ConnMysqlSlave
from db_connect import ConnMysql
import aiomysql
from sanic import response
import json
from db_connect.datetimes import DateEncoder

class Order():

    def __init__(self, user_id, merchat_id):

        self.user_id = user_id
        self.merchat_id = merchat_id


    async def result(self):

        if not (self.user_id and self.merchat_id):

            return {
                'status': -1,
                'errorCode': 201,
                'errorMsg': '参数不能为空',
                'resultBody': None,
            }


        if not (self.user_id.isdigit() and self.merchat_id.isdigit()):

            return {
                'status': -1,
                'errorCode': 202,
                'errorMsg': '参数必须为数字',
                'resultBody': None,
            }


        async with ConnMysqlSlave().pool.acquire() as c:

            await c.ping()

            async with c.cursor(aiomysql.DictCursor) as cur:

                sql = '''SELECT * FROM orders WHERE user_id=%s AND merchat_id=%s'''
                await cur.execute(sql,(self.user_id, self.merchat_id))
                select_info = await cur.fetchall()

            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': json.dumps(select_info, cls=DateEncoder),
            }
























